﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
public partial class Report_UserWeekDepartment : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    Authentication dllAuthentication = new Authentication();
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    MonthPlanManager dllMonthPlanManager = new MonthPlanManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    WeekPlanofManager dllWeekPlanofManager = new WeekPlanofManager();
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    ReportManager dllReportManager = new ReportManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpDepartment);
            //起始周的DropDownList绑定
            bindDropDownList(this.drpYearStart);
            bindDropDownList(this.drpQuarterDateStart);
            bindDropDownList(this.drpMonthDateStart);
            bindDropDownList(this.drpWeekDateStart);
            //结束周的DropDownList绑定
            bindDropDownList(this.drpYearEnd);
            bindDropDownList(this.drpQuarterDateEnd);
            bindDropDownList(this.drpMonthDateEnd);
            bindDropDownList(this.drpWeekDateEnd);
        }
    }
    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {

        //部门绑定
        if (_drp.Equals(this.drpDepartment))
        {
            Model.UserInfo objUserInfo = new Model.UserInfo();
            if (objUserInfo.UserName != "admin")
            {
                object[] _value = new object[1];
                _value[0] = objUserInfo.UserName;
                DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _value);
                if (_userTable.Rows.Count > 0)
                {
                    if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
                    {
                        ListItem _li = new ListItem(_userTable.Rows[0]["DepartmentId"].ToString(), _userTable.Rows[0]["DId"].ToString());
                        if (!this.drpDepartment.Items.Contains(_li))
                            this.drpDepartment.Items.Add(_li);
                    }
                    if (_userTable.Rows[0]["PartTimeDepartmentId"].ToString().Length > 0)
                    {
                        ListItem _li = new ListItem(_userTable.Rows[0]["PartTimeDepartmentId"].ToString(), _userTable.Rows[0]["PDid"].ToString());
                        if (!this.drpDepartment.Items.Contains(_li))
                            this.drpDepartment.Items.Add(_li);
                    }
                }
            }
            else
            {
                this.drpDepartment.DataSource = dllDepartment.getNoTree();
                this.drpDepartment.DataTextField = "DepartmentName";
                this.drpDepartment.DataValueField = "DepartmentId";
                this.drpDepartment.DataBind();
                ListItem _li = new ListItem("请选择部门");
                this.drpDepartment.Items.Insert(0, _li);
            }
        }
        //起始周的年份绑定
        if (_drp.Equals(this.drpYearStart))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYearStart.SelectedIndex = this.drpYearStart.Items.IndexOf(this.drpYearStart.Items.FindByText(_year));

        }
        //结束周的年份绑定
        if (_drp.Equals(this.drpYearEnd))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYearEnd.SelectedIndex = this.drpYearEnd.Items.IndexOf(this.drpYearEnd.Items.FindByText(_year));

        }

        //起始周的年计划周期绑定
        if (_drp.Equals(this.drpQuarterDateStart))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYearStart.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDateStart.DataSource = _quarter;
                this.drpQuarterDateStart.DataTextField = "QuarterName";
                this.drpQuarterDateStart.DataValueField = "Quarter_Id";
                this.drpQuarterDateStart.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDateStart.SelectedIndex = this.drpQuarterDateStart.Items.IndexOf(this.drpQuarterDateStart.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDateStart.DataSource = _quartertime;
                    this.drpQuarterDateStart.DataTextField = "QuarterName";
                    this.drpQuarterDateStart.DataValueField = "Quarter_Id";
                }
                #endregion
                bindDropDownList(this.drpMonthDateStart);
            }
            else
            {
                ListItem _li = new ListItem("无年周期");
                this.drpQuarterDateStart.Items.Clear();
                this.drpQuarterDateStart.Items.Add(_li);
                bindDropDownList(this.drpMonthDateStart);
            }
        }
        //结束周的年计划周期绑定
        if (_drp.Equals(this.drpQuarterDateEnd))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYearEnd.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDateEnd.DataSource = _quarter;
                this.drpQuarterDateEnd.DataTextField = "QuarterName";
                this.drpQuarterDateEnd.DataValueField = "Quarter_Id";
                this.drpQuarterDateEnd.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDateEnd.SelectedIndex = this.drpQuarterDateEnd.Items.IndexOf(this.drpQuarterDateEnd.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDateEnd.DataSource = _quartertime;
                    this.drpQuarterDateEnd.DataTextField = "QuarterName";
                    this.drpQuarterDateEnd.DataValueField = "Quarter_Id";
                }
                #endregion
                bindDropDownList(this.drpMonthDateEnd);
            }
            else
            {
                ListItem _li = new ListItem("无年周期");
                this.drpQuarterDateEnd.Items.Clear();
                this.drpQuarterDateEnd.Items.Add(_li);
                bindDropDownList(this.drpMonthDateEnd);
            }
        }

        //起始周的月计划周期绑定
        if (_drp.Equals(this.drpMonthDateStart))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYearStart.SelectedValue;
            _value[1] = this.drpQuarterDateStart.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDateStart.DataSource = _month;
                this.drpMonthDateStart.DataTextField = "MonthName";
                this.drpMonthDateStart.DataValueField = "Month_Id";
                this.drpMonthDateStart.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDateStart.SelectedIndex = this.drpMonthDateStart.Items.IndexOf(this.drpMonthDateStart.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    this.drpMonthDateStart.DataSource = _monthtime;
                    this.drpMonthDateStart.DataTextField = "MonthName";
                    this.drpMonthDateStart.DataValueField = "Month_Id";

                }
                #endregion
                bindDropDownList(this.drpWeekDateStart);
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDateStart.Items.Clear();
                this.drpMonthDateStart.Items.Add(_li);
                bindDropDownList(this.drpWeekDateStart);
            }
        }
        //结束周的月周期绑定
        if (_drp.Equals(this.drpMonthDateEnd))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYearEnd.SelectedValue;
            _value[1] = this.drpQuarterDateEnd.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDateEnd.DataSource = _month;
                this.drpMonthDateEnd.DataTextField = "MonthName";
                this.drpMonthDateEnd.DataValueField = "Month_Id";
                this.drpMonthDateEnd.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDateEnd.SelectedIndex = this.drpMonthDateEnd.Items.IndexOf(this.drpMonthDateEnd.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    this.drpMonthDateEnd.DataSource = _monthtime;
                    this.drpMonthDateEnd.DataTextField = "MonthName";
                    this.drpMonthDateEnd.DataValueField = "Month_Id";

                }
                #endregion
                bindDropDownList(this.drpWeekDateEnd);
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDateEnd.Items.Clear();
                this.drpMonthDateEnd.Items.Add(_li);
                bindDropDownList(this.drpWeekDateEnd);
            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDateStart))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYearStart.SelectedValue;
            _value[1] = this.drpQuarterDateStart.SelectedItem.Text;
            _value[2] = this.drpMonthDateStart.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                this.drpWeekDateStart.DataSource = _week;
                this.drpWeekDateStart.DataTextField = "WeekName";
                this.drpWeekDateStart.DataValueField = "Week_Id";
                this.drpWeekDateStart.DataBind();

            }
            else
            {
                ListItem _li = new ListItem("无周周期");
                this.drpWeekDateStart.Items.Clear();
                this.drpWeekDateStart.Items.Add(_li);

            }
        }
        if (_drp.Equals(this.drpWeekDateEnd))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYearEnd.SelectedValue;
            _value[1] = this.drpQuarterDateEnd.SelectedItem.Text;
            _value[2] = this.drpMonthDateEnd.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                this.drpWeekDateEnd.DataSource = _week;
                this.drpWeekDateEnd.DataTextField = "WeekName";
                this.drpWeekDateEnd.DataValueField = "Week_Id";
                this.drpWeekDateEnd.DataBind();

            }
            else
            {
                ListItem _li = new ListItem("无周周期");
                this.drpWeekDateEnd.Items.Clear();
                this.drpWeekDateEnd.Items.Add(_li);

            }
        }

    }
    #region
    /// <summary>
    /// 开始周的年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYearStart_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDateStart);
        bindDropDownList(this.drpMonthDateStart);
    }
    /// <summary>
    /// 开始周的年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDateStart_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDateStart);
    }
    /// <summary>
    /// 开始周的月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDateStart_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDateStart);
    }
    /// <summary>
    /// 开始周的周周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpWeekDateStart_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 结束周的年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYearEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDateEnd);
        bindDropDownList(this.drpMonthDateEnd);
    }
    /// <summary>
    /// 结束周的年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDateEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDateEnd);
    }
    /// <summary>
    /// 结束周的月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDateEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDateEnd);
    }
    /// <summary>
    /// 结束周的周周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpWeekDateEnd_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #endregion
    /// <summary>
    /// 查询按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        ListItem _weekSatrt = this.drpWeekDateStart.SelectedItem;
        ListItem _weekEnd = this.drpWeekDateEnd.SelectedItem;
        ListItem _type = this.drpType.SelectedItem;
        if (_weekSatrt != null && _weekEnd != null && _type != null)
        {
            #region
            //if (this.txtDepartment.Value.Length > 0)
            //{
            //    if (_type.Text == "按部门查询")
            //    {
            //        if (_weekSatrt.Text != "无月周期" && _weekEnd.Text != "无月周期")
            //        {
            //            this.gdvWeekDepartment.DataSource = dllReportManager.getUserWeekDepartmentByDepartmentNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), this.txtDepartment.Value);
            //            this.gdvWeekDepartment.DataBind();
            //            this.txtDepartment.Value = "";
            //        }
            //    }
            //    else
            //    {
            //        if (_weekSatrt.Text != "无月周期" && _weekEnd.Text != "无月周期")
            //        {
            //            this.gdvWeekDepartment.DataSource = dllReportManager.getUserWeekDepartmentByCenterNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), this.txtDepartment.Value);
            //            this.gdvWeekDepartment.DataBind();
            //            this.txtDepartment.Value = "";
            //        }
            //    }
            //}
            //else
            //{
            //    if (_type.Text == "按部门查询")
            //    {
            //        string _department = dllDepartment.getAllDepartment();
            //        if (_weekSatrt.Text != "无月周期" && _weekEnd.Text != "无月周期")
            //        {
            //            this.gdvWeekDepartment.DataSource = dllReportManager.getUserWeekDepartmentByDepartmentNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), _department);
            //            this.gdvWeekDepartment.DataBind();
            //            this.txtDepartment.Value = "";
            //        }
            //    }
            //    else
            //    {
            //        string _center = dllDepartment.getAllCenter();
            //        if (_weekSatrt.Text != "无月周期" && _weekEnd.Text != "无月周期")
            //        {
            //            this.gdvWeekDepartment.DataSource = dllReportManager.getUserWeekDepartmentByCenterNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), _center);
            //            this.gdvWeekDepartment.DataBind();
            //            this.txtDepartment.Value = "";
            //        }
            //    }
            //}
            #endregion
            string _department = string.Empty;
            if (this.drpDepartment.SelectedItem.Text == "请选择部门")
            {
                _department = dllDepartment.getAllDepartment();
            }
            else
            {
                _department = this.drpDepartment.SelectedItem.Text;
            }
            DataSet ds = dllReportManager.getUserWeekDepartmentByDepartmentNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), _department);
            this.gdvWeekDepartment.DataSource = dllReportManager.getUserWeekDepartmentByDepartmentNames(Convert.ToInt32(_weekSatrt.Value), Convert.ToInt32(_weekEnd.Value), _department);
            this.gdvWeekDepartment.DataBind(); 

        }
        MergeRow(this.gdvWeekDepartment, 0);
    }


    /// <summary>
    /// GridView行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvWeekDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (Convert.ToDouble(e.Row.Cells[2].Text) != 0)
            {
                e.Row.Cells[8].Text = Convert.ToString(Convert.ToDouble(e.Row.Cells[3].Text) /( Convert.ToDouble(e.Row.Cells[2].Text)-Convert.ToDouble(e.Row.Cells[7].Text)) * 100);
                if (e.Row.Cells[8].Text.Length > 6)
                {
                    e.Row.Cells[8].Text = e.Row.Cells[8].Text.Substring(0, 5);
                }
                e.Row.Cells[8].Text = e.Row.Cells[8].Text + "%";
            }
            else
            {
                e.Row.Cells[8].Text = "0%";
            }
        }
    }

    /// <summary>
    /// 合并同一列相同值得行
    /// </summary>
    /// <param name="_gridView"></param>
    /// <param name="_cellNum"></param>
    void MergeRow(GridView _gridView, int _cellNum)
    {
        int i = 0, rowSpanNum = 1;
        //遍历GridView
        while (i < _gridView.Rows.Count - 1)
        {
            //获得当前行
            GridViewRow gvr = _gridView.Rows[i];
            for (++i; i < _gridView.Rows.Count; i++)
            {
                //获得下一行
                GridViewRow gvrNext = _gridView.Rows[i];
                if (gvr.Cells[_cellNum].Text == gvrNext.Cells[_cellNum].Text)
                {
                    gvrNext.Cells[_cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }
                //遍历完毕进行合并
                if (i == _gridView.Rows.Count - 1)
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }

    /// <summary>
    /// Excel导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[1];
        _gridView[0] = this.gdvWeekDepartment;
        Utility.Comm.UserWeekDepartmentToExcel(GridViewToDataTable(_gridView)[0], this.drpQuarterDateStart.SelectedItem.Text + this.drpMonthDateStart.SelectedItem.Text + this.drpWeekDateStart.SelectedItem.Text + "－" + this.drpQuarterDateEnd.SelectedItem.Text + this.drpMonthDateEnd.SelectedItem.Text + this.drpWeekDateEnd.SelectedItem.Text);
    }

    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            GridViewRow gr = _sourceGridView[i].HeaderRow;
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < gr.Cells.Count; j++)
            {
                if (gr.Cells[j].Visible == true)
                {
                    dc = new DataColumn(gr.Cells[j].Text);
                    _targetDataTable[i].Columns.Add(dc);
                }
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < gr.Cells.Count; k++)
                {

                    if (gr.Cells[k].Visible == true && k < _targetDataTable[i].Columns.Count)
                    {

                        dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;


                    }

                }
                _targetDataTable[i].Rows.Add(dr);
            }

        }
        return _targetDataTable;
    }
}